import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
const daoName = "IndiLoanDiviPl";
const ButtonGroup = Button.Group;
const columns = [
    { title: '证件号码', dataIndex: 'ZJHM', key: 'ZJHM', width: 120, sorter: true, dispaly: true, filter: true, export: true },
    { title: '借据号', dataIndex: 'SFZH', key: 'SFZH', width: 120, sorter: true, dispaly: true, filter: true, export: true },
    { title: '客户名称', width: 120, sorter: true, dataIndex: 'KHMC', key: 'KHMC', dispaly: true, filter: true, export: true },
    { title: '批量方式', width: 190, sorter: true, key: 'ISTZ', dataIndex: 'ISTZCaption', dispaly: true, filter: true, export: false },
    { title: '分户日期', width: 120, sorter: true, key: 'KSSJ', caption: "KSSJ", render: data => datetime.format(data.KSSJ, "yyyy-MM-dd"), export: false, dispaly: true, filter: true },
    { title: '导入结果', width: 150, sorter: true, dataIndex: 'CLJG', key: 'CLJG', dispaly: true, filter: true, export: false },
    { title: '员工工号1', width: 120, sorter: true, key: 'KHJL1', dataIndex: 'KHJL1', dispaly: true, filter: true, export: true },
    { title: '分成比例1(%)', width: 140, sorter: true, key: 'ZB1', dataIndex: "ZB1", dispaly: true, filter: true, export: true },
    { title: '员工工号2', width: 120, sorter: true, key: 'KHJL2', dataIndex: "KHJL2", dispaly: true, filter: true, export: true },
    { title: '分成比例2(%)', width: 140, sorter: true, key: 'ZB2', dataIndex: "ZB2", dispaly: true, filter: true, export: true },
    { title: '员工工号3', width: 120, sorter: true, key: 'KHJL3', dataIndex: "KHJL3", dispaly: true, filter: true, export: true },
    { title: '分成比例3(%)', width: 140, sorter: true, dataIndex: 'ZB3', key: 'ZB3', dispaly: true, filter: true, canEdit: false, export: true },
    { title: '员工工号4', width: 120, sorter: true, key: 'KHJL4', dataIndex: "KHJL4", dispaly: true, filter: true, export: true },
    { title: '分成比例4(%)', width: 140, sorter: true, dataIndex: 'ZB4', key: 'ZB4', dispaly: true, filter: true, canEdit: false, export: true },
    { title: '员工工号5', width: 120, sorter: true, key: 'KHJL5', dataIndex: "KHJL5", dispaly: true, filter: true, export: true },
    { title: '分成比例5(%)', width: 140, sorter: true, dataIndex: 'ZB5', key: 'ZB5', dispaly: true, filter: true, canEdit: false, export: true },
];
const controls = [
    { name: 'ZJHM', required: false, type: 'text', label: '证件号码', min: 1, max: 50, grid: 12 / 24 },
    // { name: 'SFZH', required: false, type: 'text', label: '借据号', min: 1, max: 50, grid: 12 / 24 },
    {
        name: 'SFZH', required: true, type: 'search', label: '借据号', grid: 12 / 24,
        fetch: { daoName: "INDI_LOAN", searchColumns: ['Jjh'], optionTpl: 'Jjh', valueTpl: "Jjh" }
    },
    { name: 'KHMC', required: true, type: 'text', label: '客户名称', min: 1, max: 50, grid: 12 / 24 },
    {
        name: 'KSSJ', required: true, type: "date", label: '分户日期', filterAble: true, dataType: "date", min: 1, max: 50, grid: 12 / 24, placeholder: '请选择分户日期...'
    },
    { name: "ISTZ", label: "批量方式", required: true, data: [{ value: '0', label: '处理未分配客户' }, { value: '1', label: '调整已分配客户' }], grid: 12 / 24, type: "radio-group", inintValue: '0' },
    {
        name: 'KHJL1', required: true, type: 'search', label: '员工工号1', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'ZB1', required: true, type: 'number', label: '分成比例1(%)', dataType: "number", min: 1, max: 50, grid: 12 / 24 },
    {
        name: "KHJL2", required: false, type: 'search', label: '员工工号2', min: 1, max: 50, grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: "ZB2", required: false, type: 'number', label: '分成比例2(%)', dataType: "number", min: 1, max: 50, grid: 12 / 24 },
    {
        name: "KHJL3", required: false, type: 'search', label: '员工工号3', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'ZB3', required: false, type: 'number', label: '分成比例3(%)', dataType: "number", grid: 12 / 24 },
    {
        name: "KHJL4", required: false, type: 'search', label: '员工工号4', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'ZB4', required: false, type: 'number', label: '分成比例4(%)', dataType: "number", grid: 12 / 24 },
    {
        name: "KHJL5", required: false, type: 'search', label: '员工工号5', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'ZB5', required: false, type: 'number', label: '分成比例5(%)', dataType: "number", grid: 12 / 24 },
    { name: 'CLJG', required: false, ignore: true, type: 'text', label: '导入结果', grid: 12 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class INDI_LOAN_PLForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });

        let sort = {
            field: 'CLJG',
            order: 'descend'
        }
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, sorter: sort }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 20 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        this.props.dispatch({
            type: `${daoName}_DataImport/setDisplay`,
            payload: { display: true },
        });
    }
    DownTmportHandler() {
        let columnHeaders = []
        columns.map((c, j) => {
            if (!c.export) {
                return
            }
            let header = {
                Name: c.key,
                Caption: c.title,
                Width: c.width
            }
            columnHeaders.push(header)
        })
        let form = document.createElement('form');//定义一个form表单
        let whereItems = [];
        whereItems.push({
            FieldName: "1",
            Operator: "<>",
            FieldValues: "1"
        })
        const whereCauses = []
        whereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        const orderItems = []
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + daoName;
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        let data = null
        let pTypes = popedomTypes[permID]
        if (pTypes.indexOf("2") !== -1) {
            data = <ButtonGroup style={{ zIndex: 0 }}>
                <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
                <Button onClick={this.DownTmportHandler.bind(this)}>下载模板</Button>
            </ButtonGroup>
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        {data}
                    </div>
                    <font style={{ color: "red", fontSize: "12px" }}>
                        注：1、客户名称、员工1、占比1是必填项;
                        2、证件号码和借据号必须填一项;
                        3、若不存在员工2、3,则无需填写;
                        4、单元格内勿填入空格和其他非法字符;
                    </font>
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="个贷批量" importName="IndiLoanDiviPl" daoName="IndiLoanDiviPl" keys={["ID"]} paramNames={["KSSJ", "ISTZ"]} defaultParamVals={{ ISTZ: "0" }} importMode="TruncteAfterInsert" controls={controls} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoName}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
INDI_LOAN_PLForm = connect(mapStateToProps)(INDI_LOAN_PLForm)
export default INDI_LOAN_PLForm;

let render = (props) => React.createElement(INDI_LOAN_PLForm, props);
let f = new Factory()
f.set("INDI_LOAN_PLForm", render)
